import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;


public class HelloJetty
{


	public static void main(String[] args) throws Exception
	{
		Server server = new Server();
		Connector connector=new SelectChannelConnector();
		//connector.setHost("129.16.73.249");
		connector.setPort(8081);
		server.setConnectors(new Connector[]{connector});

		//Just add a similar block and implement a new abstractHandler if needed
		ContextHandler context0 = new ContextHandler();
		context0.setContextPath("/0");
		Handler handler0=new HelloHandler(0);
		context0.setHandler(handler0);

		ContextHandler context1 = new ContextHandler();
		context1.setContextPath("/1");
		Handler handler1=new HelloHandler(1);
		context1.setHandler(handler1);   

		//Add the new context to this collection
		ContextHandlerCollection contexts = new ContextHandlerCollection();
		contexts.setHandlers(new Handler[]{context0,context1});

		//The collection with contexts or handlers
		HandlerCollection handlers = new HandlerCollection();
		handlers.setHandlers(new Handler[]{contexts,new DefaultHandler()});

		server.setHandler(handlers);


		File myFile = new File ("source.txt");
		if(!myFile.exists()){
			myFile.createNewFile();
			System.err.println("file cr8ed");
		}
		System.err.println(myFile.getPath());

		FileWriter outFile = new FileWriter(myFile);
		PrintWriter out = new PrintWriter(outFile);

		// Also could be written as follows on one line
		// Printwriter out = new PrintWriter(new FileWriter(args[0]));

		// Write text to file
		out.println("11111111111111");
		out.println("22222222222222");
		out.println("33333333333333");
		out.close();



		server.start();

	}


	public static class HelloHandler extends AbstractHandler
	{
		int i;

		public HelloHandler(int i){
			this.i = i;
		}

		public void handle(String target,
				Request baseRequest,
				HttpServletRequest request,
				HttpServletResponse response) 
		throws IOException, ServletException 
		{
			System.err.println("handler "+ i);
			baseRequest.setHandled(true);
			response.setStatus(HttpServletResponse.SC_OK);

			//mimetyes for streams
			response.setContentType("application/octet-stream");

			// sendfile
			File myFile = new File ("source.txt");
			if(!myFile.exists()){
				myFile.createNewFile();
			}


			byte [] mybytearray  = new byte [(int)myFile.length()];
			FileInputStream fis = new FileInputStream(myFile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(mybytearray,0,mybytearray.length);
			OutputStream os = response.getOutputStream();
			System.out.println("Sending");
			System.err.println("Sending...");

			//write one byte at a time to calculate speed
			os.write(mybytearray,0,mybytearray.length);
			os.flush();
			System.err.println("Done");


		}


	}

}